import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../routes/center.dart';
import '../routes/home.dart';
import '../routes/mine.dart';

class BaseTabbarWidget extends StatefulWidget {
  const BaseTabbarWidget({super.key});

  @override
  State<StatefulWidget> createState() => _BaseTabbarWidgetState();
}

class _BaseTabbarWidgetState extends State<BaseTabbarWidget>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  List<Widget> list = [];
  late TabController _controller;
  int _currentIndex = 0;
  bool isStart = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    list.add(const HomePageView());
    list.add(const MinePageView());
    _controller = TabController(length: list.length, vsync: this);

    print("-----初始化页面_BottomBarWidgetState");
    Future.delayed(const Duration(seconds: 1), () {
      setState(() {
        isStart = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    print("-----build刷新页面_BottomBarWidgetState");
    // TODO: implemen_currentIndext build
    return isStart
        ? Container(
            color: Colors.lightBlueAccent,
            child: Image.network(
              "http://wx2.sinaimg.cn/mw690/ac38503ely1fesz8m0ov6j20qo140dix.jpg",
              fit: BoxFit.cover,
            ),
          )
        : Scaffold(
            body: TabBarView(
              physics: const NeverScrollableScrollPhysics(),
              controller: _controller,
              children: list,
            ),
            bottomNavigationBar: BottomNavigationBar(
              items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: const Icon(
                    Icons.home_outlined,
                  ),
                  label: "Home".tr,
                ),
                BottomNavigationBarItem(
                    icon: const Icon(
                      Icons.account_circle_outlined,
                    ),
                    label: "Mine".tr),
              ],
              type: BottomNavigationBarType.fixed,
              currentIndex: _currentIndex,
              fixedColor: Colors.red,
              onTap: (int index) {
                setState(() {
                  _currentIndex = index;
                  _controller.index = index;
                });
              },
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                Get.toNamed("/centerPage");
              },
              backgroundColor: Colors.orange,
              tooltip: "Increment",
              child: const Icon(
                Icons.add,
                color: Colors.black,
              ),
            ),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerDocked,
          );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
